import Vue from 'vue'
import VueRouter from 'vue-router'

// 导入登录组件
// import Login from '../components/Login.vue'
// 导入后台主页组件
// import Home from '../components/Home.vue'
// 导入欢迎组件
// import Welcome from '../components/Welcome.vue'
const Login = () => import(/* webpackChunkName: "Login_Home_Welcome" */ '../components/Login.vue')
const Home = () => import(/* webpackChunkName: "Login_Home_Welcome" */ '../components/Home.vue')
const Welcome = () => import(/* webpackChunkName: "Login_Home_Welcome" */ '../components/Welcome.vue')

// 导入用户列表组件
// import Users from '../components/user/Users.vue'
// 导入权限列表组件
// import Rights from '../components/power/Rights.vue'
// 导入角色列表组件
// import Roles from '../components/power/Roles.vue'
const Users = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/user/Users.vue')
const Rights = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/power/Rights.vue')
const Roles = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/power/Roles.vue')

// 导入商品分类组件
// import GoodsCate from '@/components/goods/Cate.vue'
// 导入分类参数组件
// import GoodsParams from '@/components/goods/Params.vue'
// 导入商品列表组件
// import GoodsList from '@/components/goods/List.vue'
// 导入商品添加组件
// import GoodsAdd from '@/components/goods/Add.vue'
const GoodsCate = () => import(/* webpackChunkName: "GoodsCate_GoodsParams_GoodsList_GoodsAdd" */ '@/components/goods/Cate.vue')
const GoodsParams = () => import(/* webpackChunkName: "GoodsCate_GoodsParams_GoodsList_GoodsAdd" */ '@/components/goods/Params.vue')
const GoodsList = () => import(/* webpackChunkName: "GoodsCate_GoodsParams_GoodsList_GoodsAdd" */ '@/components/goods/List.vue')
const GoodsAdd = () => import(/* webpackChunkName: "GoodsCate_GoodsParams_GoodsList_GoodsAdd" */ '@/components/goods/Add.vue')

// 导入订单列表组件
// import Orders from '@/components/order/Orders.vue'
// 导入报表组件
// import Report from '@/components/report/Report.vue'
const Orders = () => import(/* webpackChunkName: "Orders_Report" */ '@/components/order/Orders.vue')
const Report = () => import(/* webpackChunkName: "Orders_Report" */ '@/components/report/Report.vue')

Vue.use(VueRouter)

// 路由规则
const routes = [
  // 重定向的路由规则
  { path: '/', redirect: '/login' },
  // 登录的路由规则
  { path: '/login', component: Login },
  // 后台主页的路由规则
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/users', component: Users },
      { path: '/rights', component: Rights },
      { path: '/roles', component: Roles },
      { path: '/categories', component: GoodsCate },
      { path: '/params', component: GoodsParams },
      { path: '/goods', component: GoodsList },
      { path: '/goods/add', component: GoodsAdd },
      { path: '/orders', component: Orders },
      { path: '/reports', component: Report }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 为 new 出来的路由实例对象，挂载路由导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  // 如果没有被 return，证明用户想访问有权限的页面
  const tokenStr = window.sessionStorage.getItem('token')
  // 没有 token，强制跳转到登录页
  if (!tokenStr) return next('/login')
  // 有token，直接放行
  next()
})

export default router
